﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using mshtml;

namespace mfe_proj.HtmlEditor
{
    public partial class Editor : UserControl
    {
        public Editor()
        {
            InitializeComponent();
            SetupBrowser();
        }

        private void SetupBrowser()
        {
            webBrowser1.DocumentText = "<html><body></body></html>";
            doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
            doc.designMode = "On";
            webBrowser1.Document.ContextMenuShowing += new HtmlElementEventHandler(Document_ContextMenuShowing);
        }

        void Document_ContextMenuShowing(object sender, HtmlElementEventArgs e)
        {
            
        }

        void ExecuteCommand(object sender, EventArgs e)
        {
            webBrowser1.Document.ExecCommand(((ToolStripButton)sender).Tag.ToString(), true, null);
        }

        #region properties
        private IHTMLDocument2 doc;
        [Browsable(true)]
        public override string Text
        {
            get { return webBrowser1.DocumentText; }
            set { webBrowser1.DocumentText = value; }
        }
        [Browsable(false)]
        public string BodyHtml
        {
            get
            {
                if (webBrowser1.Document != null &&
                    webBrowser1.Document.Body != null)
                {
                    return webBrowser1.Document.Body.InnerHtml;
                }
                else
                    return string.Empty;
            }
            set
            {
                if (webBrowser1.Document.Body != null)
                    webBrowser1.Document.Body.InnerHtml = value;
            }
        }
        public string DocumentText
        {
            get
            {
                return webBrowser1.DocumentText;
            }
            set
            {
                webBrowser1.DocumentText = value;
            }
        }
        [Browsable(false)]
        public string BodyText
        {
            get
            {
                if (webBrowser1.Document != null &&
                    webBrowser1.Document.Body != null)
                {
                    return webBrowser1.Document.Body.InnerText;
                }
                else
                    return string.Empty;
            }
            set
            {
                if (webBrowser1.Document.Body != null)
                    webBrowser1.Document.Body.InnerText = value;
            }
        }
        #endregion

        private void btnRawHtml_Click(object sender, EventArgs e)
        {
            Forms.RawHtmlForm form = new mfe_proj.Forms.RawHtmlForm();
            form.ApplyOkClicked += new mfe_proj.Forms.RawHtmlForm.RawHtmlFormEventHandler(form_ApplyOkClicked);
            form.Html = BodyHtml;
            form.ShowDialog();
        }

        void form_ApplyOkClicked(object sender, mfe_proj.Forms.RawHtmlForm.RawHtmlEventArgs e)
        {
            BodyHtml = e.NewHtml;
        }
    }
}
